package panels;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.ArrayList;
import javax.swing.*;

import niso.Expense;
import niso.userFrame;

public class ExpensePane extends JSplitPane implements ActionListener, ItemListener, FocusListener,KeyListener{

	private userFrame Parent;
	private Date SelectedDate;
	private JButton ViewBt,ManageBt,Nextday,Prevday,NextRows,PrevRows,AddBt,MonthlyBt;
	private JTextField Rowbox;
	private JComboBox DayBox,MonthBox,YearBox;
	private CardLayout CL;
	private JPanel ViewPane;
	private boolean edit;
	private ArrayList<Expense> Expenses;
	private JLabel LBLnew,DayLbl,MonthLbl,Yearlbl,Statuslbl,NewExpenseLbl,NewCostLbl,Totlbl;
	private JTextField Text,Cost,TotalBox;
	private ArrayList<JLabel> Indexes;
	private ArrayList<JTextField>CostBoxes,Comments;
	private Container Cnt=new Container();
	private JPanel RecordPane;
	private String CurrentText;
	private int batchMark;
	
	
	public ExpensePane(userFrame P){
		Parent=P;
		SelectedDate=new Date();
		ViewBt=new JButton("View Expenses");
		ManageBt=new JButton("Manage Expenses");
		Nextday=new JButton("Next Day");
		Prevday=new JButton("Previous Day");
		NextRows=new JButton("Next");
		PrevRows=new JButton("Previous");
		MonthlyBt=new JButton("View Monthly expenses");
		
		edit=false;
		
		Rowbox=new JTextField("5");
		DayBox=new JComboBox();
		MonthBox=new JComboBox();
		YearBox=new JComboBox();
		
		generateUIComponents("Owner");
		ViewBt.addActionListener(this);
		ManageBt.addActionListener(this);
		ViewBt.setEnabled(false);
		MonthlyBt.addActionListener(this);
		DayBox.addItemListener(this);
		MonthBox.addItemListener(this);
		YearBox.addItemListener(this);
		Text.addFocusListener(this);
		Cost.addFocusListener(this);
		Rowbox.addFocusListener(this);
	}
	
	public void generateUIComponents(String userType){
		GBPane Nav;
		LBLnew=new JLabel("<html><h2>New Expense</h2></html>");
		Text=new JTextField(10);
		Cost=new JTextField("0",10);
		TotalBox=new JTextField();
		
		// Set navigation panel at the left
		Nav=new GBPane();
		Nav.addComponent(ViewBt, 0, 0, 1, 1, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.WEST);
		Nav.addComponent(MonthlyBt,0, 2, 1, 1, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.WEST);
		if(userType.matches("Owner")||userType.matches("Bookkeeper"))
			Nav.addComponent(ManageBt, 0, 1, 1, 1, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.WEST);
		setLeftComponent(Nav);
		
		
		CL=new CardLayout();
		Cnt.setLayout(CL);
		
		GBPane GBView=new GBPane();
		
		GBView.addComponent(new JLabel("<html><h1>Expenses</h1></html>"), 3, 0, 2, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(Statuslbl=new JLabel(""), 0, 12, 8, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(Prevday, 0, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(DayLbl=new JLabel("Day"), 1, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(DayBox, 2, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(MonthLbl=new JLabel("Month"), 3, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(MonthBox, 4, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(Yearlbl=new JLabel("Year"), 5, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(YearBox, 6, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(Nextday, 7, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		
		GBView.addComponent(PrevRows, 1, 3, 2, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(Rowbox, 3, 3, 2, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
		GBView.addComponent(NextRows, 5, 3, 2, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		
		setRecords("now()");
		RecordPane=new JPanel();
		if(Expenses.isEmpty()){
			Rowbox.setText("0");
			RowChage(0);
			Statuslbl.setText("No expenses recorded today");
			
		}
		else Rowbox.setText(Expenses.size()+"");
		recordPaneConfig();
		
		JScrollPane JSP=new JScrollPane(RecordPane);
		//recordPaneConfig(Integer.valueOf(Rowbox.getText()));
		JSP.setPreferredSize(new Dimension(300, 400));
		
		try{
			setComboBoxItems(MonthBox);
			setComboBoxItems(YearBox);
		} catch(SQLException SqlEx){}
		
		GBView.addComponent(JSP, 1, 5, 6, 3, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		
		GBView.addComponent(Totlbl=new JLabel("Total"), 3, 8, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(TotalBox, 4, 8, 1, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
		GBView.addComponent(NewExpenseLbl= new JLabel("Name"), 3, 10, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(NewCostLbl= new JLabel("Amount"), 3, 11, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(LBLnew, 3, 9, 2, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(Text, 4, 10, 1, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
		GBView.addComponent(Cost, 4, 11, 1, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
		GBView.addComponent(AddBt=new JButton("Add"), 5, 10, 1, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST);

		
		manageVisibility(false);
		TotalBox.setEditable(false);
		AddBt.setEnabled(false);
		AddBt.addActionListener(this);
		
		Cnt.add("View", GBView);
		setRightComponent(Cnt);
		
		Nextday.addActionListener(this);
		Prevday.addActionListener(this);
		NextRows.addActionListener(this);
		PrevRows.addActionListener(this);
		Rowbox.addKeyListener(this);
		Text.addKeyListener(this);
		Cost.addKeyListener(this);
		
	}
	
	private void recordPaneConfig(){
		CostBoxes=new ArrayList<JTextField>();
		Comments=new ArrayList<JTextField>();
		Indexes=new ArrayList<JLabel>();
		
		Statuslbl.setText("");
		RecordPane.removeAll();
		RecordPane.setLayout(new GridLayout(Expenses.size()+1, 3));
		
		if(MonthlyBt.isEnabled())
			RecordPane.add(new JLabel("Number",JLabel.RIGHT));
		else	RecordPane.add(new JLabel("Recorded on",JLabel.LEFT));
		
		RecordPane.add(new JLabel("Expense"));
		RecordPane.add(new JLabel("Cost"));
		if(Expenses.size()>0)	
			for(int ctr=0;ctr<Expenses.size();ctr++){
				Comments.add(new JTextField(Expenses.get(ctr).getComment()));
				CostBoxes.add(new JTextField(Expenses.get(ctr).getCost()+""));
				
				if(MonthlyBt.isEnabled()){
					Indexes.add(new JLabel(""+(ctr+1),JLabel.RIGHT));
					Comments.get(ctr).setEditable(edit);
					CostBoxes.get(ctr).setEditable(edit);
				}
				else	{
					Indexes.add(new JLabel(Expenses.get(ctr).getID(),JLabel.LEFT));
					Comments.get(ctr).setEditable(false);
					CostBoxes.get(ctr).setEditable(false);
				}
				
			
				RecordPane.add(Indexes.get(ctr));
				RecordPane.add(Comments.get(ctr));
				RecordPane.add(CostBoxes.get(ctr));
				
				//Comments.get(ctr).setEditable(edit);
				//CostBoxes.get(ctr).setEditable(edit);
			
				TotalBox.setText(""+sumofExpense());
				
				Comments.get(ctr).addFocusListener(this);
				CostBoxes.get(ctr).addFocusListener(this);
				Comments.get(ctr).addKeyListener(this);
				CostBoxes.get(ctr).addKeyListener(this);
			}
		else {
			Statuslbl.setText("<html><h2>No Expenses Listed on this day</h2></html>");
			TotalBox.setText("0");
		}
		batchMark=Expenses.size();
	}
	
	private void recordPaneConfig(int rows){
		int ctr;
		CostBoxes=new ArrayList<JTextField>();
		Comments=new ArrayList<JTextField>();
		Indexes=new ArrayList<JLabel>();
		
		RecordPane.removeAll();
		if(rows>0&&Expenses.size()>0){
		RecordPane.setLayout(new GridLayout(rows+1, 3));
		RecordPane.add(new JLabel("Number"));
		RecordPane.add(new JLabel("Expense"));
		RecordPane.add(new JLabel("Cost"));
		for(ctr=0;ctr<Expenses.size();ctr++){
			Indexes.add(new JLabel(""+(ctr+1)));
			Comments.add(new JTextField(Expenses.get(ctr).getComment()));
			CostBoxes.add(new JTextField(Expenses.get(ctr).getCost()+""));
		}
		
		for(ctr=0;ctr<rows;ctr++){
			RecordPane.add(Indexes.get(ctr));
			RecordPane.add(Comments.get(ctr));
			RecordPane.add(CostBoxes.get(ctr));
			Comments.get(ctr).setEditable(edit);
			CostBoxes.get(ctr).setEditable(edit);
			
			Comments.get(ctr).addFocusListener(this);
			CostBoxes.get(ctr).addFocusListener(this);
			Comments.get(ctr).addKeyListener(this);
			CostBoxes.get(ctr).addKeyListener(this);
		}
		TotalBox.setText(""+sumofExpense());
		batchMark=ctr;
		}
		else Statuslbl.setText("<html><h2>No Expenses Listed on this day</h2></html>");
		TotalBox.setText("0");
	}
	
	
	
	public void manageVisibility(boolean b){
		LBLnew.setVisible(b);
		Text.setVisible(b);
		Cost.setVisible(b);
		AddBt.setVisible(b);
		NewExpenseLbl.setVisible(b);
		NewCostLbl.setVisible(b);
		
		Prevday.setVisible(!b);
		Nextday.setVisible(!b);
		DayLbl.setVisible(!b);
		MonthLbl.setVisible(!b);
		Yearlbl.setVisible(!b);
		DayBox.setVisible(!b);
		MonthBox.setVisible(!b);
		YearBox.setVisible(!b);
		
		
		for(int ctr=0;ctr<Expenses.size();ctr++){
			Comments.get(ctr).setEditable(edit);
			CostBoxes.get(ctr).setEditable(edit);
			
		}
	}
	
	
	public void setupmonthlyReport(){
		
		ViewBt.setEnabled(true);
		ManageBt.setEnabled(true);
		MonthlyBt.setEnabled(false);
		LBLnew.setVisible(false);
		Text.setVisible(false);
		Cost.setVisible(false);
		AddBt.setVisible(false);
		NewExpenseLbl.setVisible(false);
		NewCostLbl.setVisible(false);
		
		Prevday.setVisible(false);
		Nextday.setVisible(false);
		DayLbl.setVisible(false);
		MonthLbl.setVisible(true);
		Yearlbl.setVisible(true);
		DayBox.setVisible(false);
		MonthBox.setVisible(true);
		YearBox.setVisible(true);
		
		getMonthlyExpenses(MonthBox.getSelectedIndex()+1);
		
		
		recordPaneConfig();
		Rowbox.setText(Expenses.size()+"");
		
		for(int ctr=0;ctr<Expenses.size();ctr++){
			Comments.get(ctr).setEditable(false);
			CostBoxes.get(ctr).setEditable(false);
			
		}
		paintAnsestors(RecordPane);
	}
	
	public void Insert(){
		int i=Parent.getDBHandler().MySQLinsert("INSERT INTO Expense values (now(),'"+Cost.getText()+"','"+Text.getText()+"');",Statuslbl);
		
		if(i>0){
			setRecords("now()");
			//recordPaneConfig(Integer.valueOf(Rowbox.getText()));
			paintAnsestors(RecordPane);
			Cost.setText("0");
			Text.setText("");
			AddBt.setEnabled(false);
			recordPaneConfig();
			Rowbox.setText(""+Expenses.size());
		}
	}
	
	public void setRecords(String s){
		Expenses=new ArrayList<Expense>();
		ResultSet RS;
		
		if(s.endsWith("()")||s.endsWith("())"))
			RS=Parent.getDBHandler().MySQLSelect("SELECT * from Expense where Date(ExpenseTime)=Date("+s+");");
		else RS=Parent.getDBHandler().MySQLSelect("SELECT * from Expense where Date(ExpenseTime)='"+s+"';");
		
		try{
			while(RS.next())
				Expenses.add(new Expense(RS.getString(1),RS.getString(3),RS.getDouble(2)));
			RS.close();
		}catch (SQLException SQLe) {
			// TODO Auto-generated catch block
			Statuslbl.setText("Error in collecting data");
		}
		
	}
	
	
	public void getMonthlyExpenses(int m){
		Expenses=new ArrayList<Expense>();
		ResultSet RS;
		
		RS=Parent.getDBHandler().MySQLSelect("SELECT * from Expense where Month(ExpenseTime)='"+m+"';",Statuslbl);
		try{
			while(RS.next())
				Expenses.add(new Expense(RS.getString(1),RS.getString(3),RS.getDouble(2)));
			
			RS.close();
		}catch (SQLException SQLe) {
			// TODO Auto-generated catch block
			Statuslbl.setText("Error in collecting data");
		}
	}
	
	public void setComboBoxItems(JComboBox cb) throws SQLException{
		int limit,ctr;
		if(cb.getItemCount()>0)
			cb.removeAllItems();
		if(cb==MonthBox){
			cb.addItem("January");cb.addItem("Febuary");cb.addItem("March");
			cb.addItem("April");cb.addItem("May");cb.addItem("June");
			cb.addItem("July");cb.addItem("August");cb.addItem("September");
			cb.addItem("October");cb.addItem("November");cb.addItem("December");
			
			ResultSet RS=Parent.getDBHandler().MySQLSelect("Select Month(now());"),RY,RD;
			RS.first();
			cb.setSelectedIndex(RS.getInt(1)-1);
			
			RY=Parent.getDBHandler().MySQLSelect("Select Year(now());");
			RY.first();
			
			
			
			if(RS.getInt(1)==1||RS.getInt(1)==3||RS.getInt(1)==5||RS.getInt(1)==7||RS.getInt(1)==8
					||RS.getInt(1)==10||RS.getInt(1)==12)
				limit=31;
			else if(RS.getInt(1)==2)
				if(RY.getInt(1)%4!=0)
					limit =28;
				else limit=29;
			else limit=30;
			
			for(ctr=1;ctr<=limit;ctr++)
				DayBox.addItem(""+ctr);
			RS.close();
			RY.close();
			
			
			RD=Parent.getDBHandler().MySQLSelect("Select Day(now());");
			RD.first();
			
			DayBox.setSelectedItem(RD.getString(1));
			RD.close();
			
			}
		else if(cb==YearBox){
			ResultSet RS=Parent.getDBHandler().MySQLSelect("Select Year(TimeRecorded) from Inventory group by year(TimeRecorded);");
			
			while(RS.next())
				cb.addItem(RS.getString(1));
			cb.setSelectedIndex(cb.getItemCount()-1);
			RS.close();
		}
		else if(cb==DayBox){
			if(MonthBox.getSelectedIndex()==0||MonthBox.getSelectedIndex()==2||MonthBox.getSelectedIndex()==4||MonthBox.getSelectedIndex()==6||
					MonthBox.getSelectedIndex()==7||MonthBox.getSelectedIndex()==9||MonthBox.getSelectedIndex()==11)
				limit=31;
			else if(MonthBox.getSelectedIndex()==1){
				if(Integer.valueOf(YearBox.getSelectedItem().toString())%4!=0)
					limit=28;
				else limit=29;
			}
			else limit=30;
			
			for(ctr=1;ctr<=limit;ctr++)
				DayBox.addItem(""+ctr);
		}
		
	}

	public void RowChage(int rows){
		Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
		if(rows>Expenses.size()){
			rows=Expenses.size();
			Statuslbl.setText("Rows exceed number of records");
		}
		if(rows<0){
			rows=0;
			Statuslbl.setText("Must have at least 0 row");
		}
		Rowbox.setText(""+rows);
		recordPaneConfig(rows);
	}
	
	//method that changes the DayBox based on the month
	public void DayBoxConfig(int month){
		int limit,year=Integer.valueOf(YearBox.getSelectedItem().toString()),MaxDay=DayBox.getItemCount(),Day=DayBox.getSelectedIndex()+1;
		//Determine month to determine number of days
		if(month==0||month==2||month==4||month==6||month==7||month==9||month==11)
			limit =31;
		else if(month==1)
			if(year%4!=0)
				limit =28;
			else limit=29;
		else limit=30;
		
		DayBox.removeItemListener(this);
		//DayBox.setSelectedIndex(0);
		
		if(MaxDay<limit)
			for(int ctr=MaxDay+1;ctr<=limit;ctr++)
				DayBox.addItem(""+ctr);
		else if(MaxDay>limit){
			DayBox.removeAllItems();
			for(int ctr=1;ctr<=limit;ctr++)
				DayBox.addItem(""+ctr);
			while(Day>limit)
				Day--;
			DayBox.setSelectedItem(""+Day);
		}
		MonthBox.addItemListener(this);
		DayBox.addItemListener(this);
	}
	
	//Method that responds to the next and previous day buttons
	public void NextPrevDay(int change){
		int month;
		if(change>0){
			if(DayBox.getSelectedIndex()<DayBox.getItemCount()-change)
				DayBox.setSelectedIndex(DayBox.getSelectedIndex()+change);
			else if(MonthBox.getSelectedIndex()<MonthBox.getItemCount()-change){
				MonthBox.removeItemListener(this);
				MonthBox.setSelectedIndex(MonthBox.getSelectedIndex()+change);
				month=MonthBox.getSelectedIndex();
				
				DayBoxConfig(month);
				
				
				DayBox.setSelectedIndex(0);
			}
			else{
				YearBox.removeItemListener(this);
				YearBox.addItem(""+(Integer.valueOf(YearBox.getSelectedItem().toString())+1));
				YearBox.setSelectedIndex(YearBox.getItemCount()-1);
				MonthBox.setSelectedIndex(0);
				DayBox.setSelectedIndex(0);
				YearBox.addItemListener(this);
				
			}
			
		}
		else if(change<0){
			if(DayBox.getSelectedIndex()>0)
				DayBox.setSelectedIndex(DayBox.getSelectedIndex()+change);
			else if(MonthBox.getSelectedIndex()>0){
				MonthBox.removeItemListener(this);
				MonthBox.setSelectedIndex(MonthBox.getSelectedIndex()+change);
				month=MonthBox.getSelectedIndex();
				DayBoxConfig(month);
				
				
				DayBox.setSelectedIndex(DayBox.getItemCount()-1);
			}
			else if(YearBox.getSelectedIndex()>0)
				YearBox.setSelectedIndex(YearBox.getSelectedIndex()+change);
			else Statuslbl.setText("No more record in the previous Years");
		}
	}
	
	
	public double sumofExpense(){
		double Total=0;
		
		for(int ctr=0;ctr<Expenses.size();ctr++){
			Total+=Expenses.get(ctr).getCost();
		}
		return Total;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent AE) {
		if(AE.getSource()==ManageBt){
			edit=true;
			ManageBt.setEnabled(false);
			ViewBt.setEnabled(true);
			MonthlyBt.setEnabled(true);
			setRecords("now()");
			recordPaneConfig();
			Rowbox.setText(Expenses.size()+"");
			paintAnsestors(RecordPane);
			manageVisibility(edit);
		}
		else if(AE.getSource()==ViewBt){
			edit=false;
			ViewBt.setEnabled(false);
			ManageBt.setEnabled(true);
			MonthlyBt.setEnabled(true);
			setRecords(YearBox.getSelectedItem().toString()+"-"+(MonthBox.getSelectedIndex()+1)+"-"+DayBox.getSelectedItem().toString());
			manageVisibility(edit);
			Rowbox.setText(Expenses.size()+"");
			recordPaneConfig();
		}
		else if(AE.getSource()==MonthlyBt)
			setupmonthlyReport();

		else if(AE.getSource()==AddBt)
			Insert();
		else if(AE.getSource()==Nextday)
			NextPrevDay(1);
		else if(AE.getSource()==Prevday)
			NextPrevDay(-1);
			//DayBox.setSelectedIndex(DayBox.getSelectedIndex()-1);
		else if(AE.getSource()==NextRows)
			InfoBatches(true);
		else if(AE.getSource()==PrevRows)
			InfoBatches(false);
		
		
		
	}
	
	private void InfoBatches(boolean next){
		int rows=Integer.valueOf(Rowbox.getText()),i=0;
		
		if(next&&rows<Expenses.size()){
			while(batchMark>=0&&batchMark+rows>Expenses.size())
				batchMark--;
			if(batchMark+rows<=Expenses.size()){
				for(int ctr=batchMark;ctr<rows+batchMark;ctr++){
					Indexes.get(i).setText(""+(ctr+1));
					Comments.get(i).setText(Expenses.get(ctr).getComment());
					CostBoxes.get(i).setText(Expenses.get(ctr).getCost()+"");
					i++;
				}
				batchMark+=rows;
				
				for(int c=i;c<Indexes.size();c++){
					Indexes.get(i).setText("");
					Comments.get(i).setText("");
					CostBoxes.get(i).setText("");
					i++;
				}
			}
		}
		else if(rows<Expenses.size()){
			batchMark-=rows;
			while(batchMark-rows<0)
				batchMark++;
			if(batchMark-rows>=0){
				int ctr;
				i=rows;
				for(ctr=batchMark;ctr>=batchMark-rows;ctr--){
					Indexes.get(i).setText(""+(ctr+1));
					Comments.get(i).setText(Expenses.get(ctr).getComment());
					CostBoxes.get(i).setText(Expenses.get(ctr).getCost()+"");
					i--;
				}
				//batchMark+=rows;
			}
			
		}
	}

	
	public void DeleteRecord(int i){
		
		Parent.getDBHandler().MySQLUpdate("DELETE FROM Expense where ExpenseTime='"+Expenses.get(i).getID()+"';",Statuslbl);
		Expenses.remove(i);
		recordPaneConfig(Integer.valueOf(Rowbox.getText())-1);
                Rowbox.setText(""+(Integer.valueOf(Rowbox.getText())-1));
                TotalBox.setText(""+sumofExpense());
		paintAnsestors(RecordPane);
	}
	
	
	public void paintAnsestors(Container cn){
		Container C=cn;
		while(C.getParent()!=null){
			C.repaint();
			C=C.getParent();
		}
	}
	
	@Override
	public void itemStateChanged(ItemEvent IE) {
		if(IE.getSource()==MonthBox){
			MonthBox.removeItemListener(this);
			DayBoxConfig(MonthBox.getSelectedIndex());
			
		}
		if((IE.getSource()==DayBox||IE.getSource()==MonthBox||IE.getSource()==YearBox)&&MonthlyBt.isEnabled()){
			setRecords(YearBox.getSelectedItem().toString()+"-"+(MonthBox.getSelectedIndex()+1)+"-"+DayBox.getSelectedItem().toString());
			recordPaneConfig();
			Rowbox.setText(Expenses.size()+"");
			
			//if(IE.getSource()==MonthBox)
				
			//RecordPane.repaint();
			
			paintAnsestors(RecordPane);
		}
		else if(!MonthlyBt.isEnabled()&&IE.getSource()==MonthBox)
			setupmonthlyReport();
			
		
			//TBL.changedDate(YearBox.getSelectedItem().toString()+"-"+MonthBox.getSelectedIndex()+1+"-"+DayBox.getSelectedItem().toString());
	}

	@Override
	public void focusGained(FocusEvent FE) {
		int i;
		
		if(Comments.contains(FE.getSource())){
			i=Comments.indexOf(FE.getSource());
			CurrentText=Comments.get(i).getText();
		}
		else if(CostBoxes.contains(FE.getSource())){
			i=CostBoxes.indexOf(FE.getSource());
			CurrentText=CostBoxes.get(i).getText();
		}
		else if(FE.getSource()==Text)
			CurrentText=Text.getText();
		else if(FE.getSource()==Cost)
			CurrentText=Cost.getText();
		
		
		
	}

	@Override
	public void focusLost(FocusEvent FE) {
		int i;
		JTextField jText=null;
		
		Statuslbl.setText("");
			if(Comments.contains(FE.getSource())){
				try{
					i=Comments.indexOf(FE.getSource());
					jText=Comments.get(i);
					
					if(Comments.get(i).getText().length()==0){
						Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
						Statuslbl.setText("Do not put empty comments!");
						jText.setText(CurrentText);
					}
					else if(CurrentText!=Comments.get(i).getText())
					
						Parent.getDBHandler().MySQLUpdate("Update Expense SET Comment='"+Comments.get(i).getText()+"' where ExpenseTime='"+Expenses.get(i).getID()+"';");
						
				}catch(SQLException sqlEX){
					Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
					Statuslbl.setText("Problem: "+sqlEX);
					if(jText!=null)
						jText.setText(CurrentText);
				}
			}
			else if(CostBoxes.contains(FE.getSource())){
				double amt=0;
				i=CostBoxes.indexOf(FE.getSource());
				try{
					amt=Double.valueOf(CostBoxes.get(i).getText());
				}catch(NumberFormatException sqlEX){
					Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
					Statuslbl.setText("Type numbers only!");
					CostBoxes.get(i).setText(CurrentText);
					amt=Expenses.get(i).getCost();
				}
					
					jText=CostBoxes.get(i);
					
					if(CostBoxes.get(i).getText().length()==0){
						Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
						Statuslbl.setText("Do not leave the cost empty!");
						jText.setText(CurrentText);
					}
					else if(CurrentText!=CostBoxes.get(i).getText())
						try {
							if(amt<0){
								amt*=-1;
								CostBoxes.get(i).setText(""+amt);
								Statuslbl.setText("Please type positive numbers");
							}
							Parent.getDBHandler().MySQLUpdate("Update Expense SET Cost='"+amt+"' where ExpenseTime='"+Expenses.get(i).getID()+"';");
							Expenses.get(i).setCost(amt);
							TotalBox.setText(""+sumofExpense());
						} catch (SQLException e) {
							Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
							Statuslbl.setText("Type numbers only!");
						}
			}
			
			else if(FE.getSource()==Text||FE.getSource()==Cost){
				double amt=0;
				
				try{
					amt=Double.valueOf(Cost.getText());
				}catch(NumberFormatException NE){
					amt=0;
					Cost.setText(""+amt);
					Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
					Statuslbl.setText("Amount must be in numbers");
				}
				
				if(Text.getText().length()>0&&Cost.getText().length()>0&&amt>=0)
					AddBt.setEnabled(true);
				else AddBt.setEnabled(false);
			}
			else if(FE.getSource()==Rowbox){
				Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
				try{
					if(Integer.valueOf(Rowbox.getText())>0)
						RowChage(Integer.valueOf(Rowbox.getText()));
					else if(Integer.valueOf(Rowbox.getText())==0)
						Statuslbl.setText("0 rows dispalyed.");
					else {
						Statuslbl.setText("No negative numbers");
						Rowbox.setText(""+Comments.size());
					}
				}catch(NumberFormatException NF){
					Statuslbl.setText("Please enter a valid number");
					Rowbox.setText(""+Comments.size());
				}
			}
		
		CurrentText="";
	}

	@Override
	public void keyPressed(KeyEvent KE) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent KE) {
		int i;
		Statuslbl.setText("");
		if(KE.getSource()==Rowbox&&KE.getKeyCode()==KeyEvent.VK_ENTER){
			Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
		try{
			if(Integer.valueOf(Rowbox.getText())>=0){
				RowChage(Integer.valueOf(Rowbox.getText()));
				if(Integer.valueOf(Rowbox.getText())==0)
					Statuslbl.setText("0 rows dispalyed.");
			}
			else {
				Statuslbl.setText("No negative numbers");
				Rowbox.setText(""+Comments.size());
			}
		}catch(NumberFormatException NF){
			Statuslbl.setText("Please enter a valid number");
			Rowbox.setText(""+Comments.size());
			}
		paintAnsestors(RecordPane);
		}
		else if(Comments.contains(KE.getSource())&&KE.getKeyCode()==KeyEvent.VK_ENTER){
			i=Comments.indexOf(KE.getSource());
			CostBoxes.get(i).requestFocus();
		}
		else if(CostBoxes.contains(KE.getSource())&&KE.getKeyCode()==KeyEvent.VK_ENTER){
			i=CostBoxes.indexOf(KE.getSource());
			if(i<Comments.size()-1)
				Comments.get(i+1).requestFocus();
		}
		else if(KE.getSource()==Text&&KE.getKeyCode()==KeyEvent.VK_ENTER)
			Cost.requestFocus();
		else if(KE.getSource()==Cost&&KE.getKeyCode()==KeyEvent.VK_ENTER){
			if(Text.getText().length()>0)
				AddBt.requestFocus();
			else Text.requestFocus();
		}
		else if(CostBoxes.contains(KE.getSource())&&KE.getKeyCode()==KeyEvent.VK_DELETE)
			DeleteRecord(CostBoxes.indexOf(KE.getSource()));
		else if(Comments.contains(KE.getSource())&&KE.getKeyCode()==KeyEvent.VK_DELETE)
			DeleteRecord(Comments.indexOf(KE.getSource()));
	}

	@Override
	public void keyTyped(KeyEvent KE) {
		// TODO Auto-generated method stub
		
	}
}
